package com.mlick.a.likou;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lixiangxin
 * @date 2020/9/7 14:55
 * 1. 两数之和
 **/
public class LK00001 {
    class Solution {
        public int[] twoSum(int[] nums, int target) {

            for (int i = 0; i < nums.length; i++) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] == target - nums[i]) {
                        return new int[]{i, j};
                    }
                }
            }

            return new int[]{};
        }

        public int[] twoSum2(int[] nums, int target) {

            Map<Integer, Integer> sources = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                int complete = target - nums[i];
                if (sources.containsKey(complete)) {
                    return new int[]{sources.get(complete), i};
                }
                sources.put(nums[i], i);
            }

            return new int[]{};
        }
    }
}
